//
//  OtherGCDViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/15.
//

#import "OtherGCDViewController.h"
#import <Masonry/Masonry.h>

@interface OtherGCDViewController ()

@end

@implementation OtherGCDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"其他用法";
    
    self.view.backgroundColor = [UIColor whiteColor];
  
    //延时函数
    UIButton *barrierButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [barrierButton setTitle:@"延时函数" forState:(UIControlStateNormal)];
    [barrierButton addTarget:self action:@selector(gcdAfter) forControlEvents:(UIControlEventTouchUpInside)];
    barrierButton.backgroundColor =[UIColor purpleColor];
    barrierButton.tag = 1001;
    [self.view addSubview:barrierButton];
    [barrierButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(150);
    }];
    
    
    //遍历方法
    UIButton *applyButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [applyButton setTitle:@"遍历方法" forState:(UIControlStateNormal)];
    [applyButton addTarget:self action:@selector(apply) forControlEvents:(UIControlEventTouchUpInside)];
    applyButton.backgroundColor =[UIColor purpleColor];
    applyButton.tag = 1001;
    [self.view addSubview:applyButton];
    [applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(barrierButton.mas_bottom).offset(15);
    }];
    
    
    
    
}

//GCD的延时方法
-(void)gcdAfter{
    
    NSLog(@"1---------%@",[NSThread currentThread]);
    
    NSLog(@"begin-running");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        NSLog(@"2---------%@",[NSThread currentThread]);
    });

}

/**
 * 一次性代码（只执行一次）dispatch_once
 */
- (void)once {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 只执行 1 次的代码（这里面默认是线程安全的）
    });
}

//快速遍历的方法 https://www.jianshu.com/p/10abdb015aac
-(void)apply{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"______begin_____");
    
    dispatch_apply(6, queue, ^(size_t index) {
     
        NSLog(@"---------%zd-----%@",index,[NSThread currentThread]);
        
    });
    
    NSLog(@"______end_____");
    
    
    
    
}

@end
